<template>
    <div class="EditDegreePopup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
				<el-form-item label="学位" prop="Name">
				    <el-input v-model="formData.Name" placeholder="请输入学位" clearable />
				</el-form-item>
				<el-form-item label="入学日期" prop="StartDate">
                    <el-date-picker v-model="formData.StartDate" type="date" />
                </el-form-item>
				<el-form-item label="授予日期" prop="GetDate">
                    <el-date-picker v-model="formData.GetDate" type="date" />
                </el-form-item>
				<el-form-item label="授予国家" prop="Nationality">
				    <el-select class="w-[280px]" v-model="formData.Country">
				        <el-option v-for="(item, key) in optionData.Nationality" :key="key" :label="item.name"
				            :value="item.value" />
				    </el-select>
				</el-form-item> 
				<el-form-item label="授予单位" prop="WorkUnit">
                    <el-input v-model="formData.WorkUnit" placeholder="请输入授予单位" clearable />
                </el-form-item>
				<el-form-item label="专业名称" prop="Professional">
                    <el-input v-model="formData.Professional" placeholder="请输入专业名称" clearable />
                </el-form-item>
				 <el-form-item label="证书编号" prop="CertificatNumber">
                    <el-input v-model="formData.CertificatNumber" placeholder="请输入证书编号" clearable />
                </el-form-item>
				<el-form-item label="学位说明" prop="Remark">
				    <el-input v-model="formData.Remark" clearable />
				</el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="EditDegreePopup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictData } from '@/hooks/useDictOptions'
import { EditDegrees } from '@/api/consumer'
const { dictData: optionData } = useDictData('Nationality')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑学位信息' : '添加学位信息'
})
console.log('数据列表',optionData)
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
    id: 0,
    member_id: props.member_id,
     Name: '',
	 StartDate: '',
     GetDate: '',
     Country: '',
     WorkUnit: '',
     Professional: '',
     CertificatNumber: '',
     Remark: ''
})

const formRules = reactive({
     Name: [
         {
             required: true,
             message: '请输入学位',
             trigger: ['blur']
         }
     ],
	 StartDate: [
	      {
	          required: true,
	         message: '请选择入学日期',
	          trigger: ['blur']
	      }
	  ],
	 GetDate: [
	      {
	          required: true,
	         message: '请选择授予日期',
	          trigger: ['blur']
	      }
	  ],
     Country: [
        {
             required: true,
             message: '请输入授予国家',
             trigger: ['blur']
         }
     ],
     WorkUnit: [
        {
             required: true,
             message: '请输入授予单位',
             trigger: ['blur']
         }
     ],
     Professional: [
         {
             required: true,
             message: '请选择专业名称',
             trigger: ['blur']
         }
	 ],
    CertificatNumber: [
        {
             required: true,
             message: '请选择学位证书编号',
             trigger: ['blur']
         }
     ]
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('学位表单内容', formData)
    await EditDegrees(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formData.id = row.id
	//console.log('读取学位开始', row)
    const data = await EditDegrees({
        id: row.id,
    		 mode:'get'
    })
	//console.log('读取学位表单内容', data)
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
